package com.xzavier.yunlian_interview.controller;

import com.xzavier.yunlian_interview.view.StudyDuration_RequestParams;
import com.xzavier.yunlian_interview.service.ex.StudyDurationException;
import com.xzavier.yunlian_interview.service.StudyDurationService;
import com.xzavier.yunlian_interview.utils.JsonResult;
import com.xzavier.yunlian_interview.view.StudyHistory;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("interview/time")
public class StudyDurationController {
    @Autowired
   private StudyDurationService studyDurationService;

    @PostMapping("tracking")
    public JsonResult<Void> recordStudyDuration(HttpServletRequest request, @RequestBody StudyDuration_RequestParams[] arr) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            throw new StudyDurationException("token缺失");
        }
        studyDurationService.recordStudyDuration(userId, arr);
        return new JsonResult<Void>(200);
    }

    @GetMapping("history")
    public JsonResult<StudyHistory> getStudyHistory(HttpServletRequest request) {
        String userId = (String) request.getAttribute("userId");
        if (userId == null) {
            throw new StudyDurationException("token缺失");
        }
        StudyHistory studyDuration = studyDurationService.getStudyDuration(userId);
        return new JsonResult<StudyHistory>(200, studyDuration);

    }
}
